package com.ysy.tz.core.selfcomp;

import com.ysy.tz.core.utils.FileResourceLoader;
import java.io.IOException;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;

public class CustomerToggleGroup extends StackPane {
    private ObjectProperty<ToggleDirection> direction = new SimpleObjectProperty();
    private DoubleProperty spacing = new SimpleDoubleProperty();
    private DoubleProperty controlBtnSize = new SimpleDoubleProperty(10.0);
    private ImageView prevButton = new ImageView();
    private ImageView nextButton = new ImageView();
    private Pane contentPane = new Pane();
    private ToggleMovePane movePane = new ToggleMovePane();
    private ObjectProperty<Node> context = new SimpleObjectProperty();
    private double oldLocation = 0.0;
    public double mousePoint = 0.0;
    public double offset = 0.0;
    private int preOffset = 0;
    private int nextOffset = 0;

    public CustomerToggleGroup() {
        this.direction.addListener((observable, oldValue, newValue) -> {
            if (newValue == ToggleDirection.VERTICAL) {
                try {
                    this.prevButton.setImage(new Image(FileResourceLoader.loadInputStream("/views/images/arrowUp.png")));
                    this.nextButton.setImage(new Image(FileResourceLoader.loadInputStream("/views/images/arrowDown.png")));
                } catch (IOException var5) {
                    var5.printStackTrace();
                }

                this.initControllBtn();
            }

        });
        this.contentPane.getChildren().add(this.movePane);
        this.getChildren().add(this.prevButton);
        this.getChildren().add(this.contentPane);
        this.getChildren().add(this.nextButton);
    }

    private void initControllBtn() {
        this.prevButton.setOnMousePressed((event) -> {
            this.preOffset = 1;
            this.requestLayout();
        });
        this.prevButton.setOnMouseReleased((event) -> {
            this.preOffset = 0;
            this.requestLayout();
        });
        this.nextButton.setOnMousePressed((event) -> {
            this.nextOffset = 1;
            this.requestLayout();
        });
        this.nextButton.setOnMouseReleased((event) -> {
            this.nextOffset = 0;
            this.requestLayout();
        });
    }

    protected void layoutChildren() {
        Insets padding = this.getPadding();
        double width = this.getWidth();
        double height = this.getHeight();
        if (this.direction.get() == ToggleDirection.VERTICAL) {
            double unitSize = width - padding.getLeft() - padding.getRight();
            this.prevButton.resizeRelocate(padding.getLeft() + (double)this.preOffset, padding.getTop() + (double)this.preOffset, unitSize, this.controlBtnSize.get());
            this.prevButton.setFitWidth(unitSize);
            this.prevButton.setFitHeight(this.controlBtnSize.get());
            this.contentPane.resizeRelocate(padding.getLeft(), padding.getTop() + this.controlBtnSize.get() + this.spacing.get(), unitSize, height - this.controlBtnSize.get() * 2.0 - padding.getTop() - padding.getBottom() - this.spacing.get() * 2.0);
            this.contentPane.setClip(new Rectangle(unitSize, height - this.controlBtnSize.get() * 2.0 - padding.getTop() - padding.getBottom() - this.spacing.get() * 2.0));
            this.movePane.setLayoutX(0.0);
            this.movePane.setPrefWidth(this.contentPane.getWidth());
            this.movePane.setPrefHeight((double)this.movePane.getChildren().size() * this.movePane.getWidth() + (double)(this.movePane.getChildren().size() - 1) * this.spacing.get());
            if (this.preOffset != 0 && this.movePane.getPrefHeight() + this.movePane.getLayoutY() > this.contentPane.getHeight()) {
                double y = -this.movePane.getLayoutY();
                this.offset = -(Math.ceil(-this.movePane.getLayoutY() / (unitSize + this.spacing.get())) + 1.0) * (unitSize + this.spacing.get());
                if (this.movePane.getPrefHeight() + this.offset < this.contentPane.getHeight()) {
                    this.offset = this.contentPane.getHeight() - this.movePane.getPrefHeight();
                }
            } else if (this.nextOffset != 0 && this.movePane.getLayoutY() < 0.0) {
                this.offset = -(Math.ceil(-this.movePane.getLayoutY() / (unitSize + this.spacing.get())) - 1.0) * (unitSize + this.spacing.get());
                if (this.movePane.getLayoutY() > 0.0) {
                    this.offset = 0.0;
                }
            }

            this.movePane.setLayoutY(this.offset);
            this.nextButton.resizeRelocate(padding.getLeft() + (double)this.nextOffset, height - padding.getBottom() - this.controlBtnSize.get() + (double)this.nextOffset, unitSize, this.controlBtnSize.get());
            this.nextButton.setFitWidth(unitSize);
            this.nextButton.setFitHeight(this.controlBtnSize.get());
        }

    }

    public ToggleDirection getDirection() {
        return (ToggleDirection)this.direction.get();
    }

    public ObjectProperty<ToggleDirection> directionProperty() {
        return this.direction;
    }

    public void setDirection(ToggleDirection direction) {
        this.direction.set(direction);
    }

    public double getSpacing() {
        return this.spacing.get();
    }

    public DoubleProperty spacingProperty() {
        return this.spacing;
    }

    public void setSpacing(double spacing) {
        this.spacing.set(spacing);
    }

    public double getControlBtnSize() {
        return this.controlBtnSize.get();
    }

    public DoubleProperty controlBtnSizeProperty() {
        return this.controlBtnSize;
    }

    public void setControlBtnSize(double controlBtnSize) {
        this.controlBtnSize.set(controlBtnSize);
    }

    public Node getContext() {
        return (Node)this.context.get();
    }

    public ObjectProperty<Node> contextProperty() {
        return this.context;
    }

    public void resetLayout() {
        if (this.direction.get() == ToggleDirection.VERTICAL) {
            if (this.movePane.getLayoutY() > 0.0) {
                this.offset = 0.0;
                this.requestLayout();
            } else if (this.movePane.getLayoutY() < 0.0 && this.movePane.getPrefHeight() + this.movePane.getLayoutY() < this.contentPane.getHeight()) {
                if (this.movePane.getPrefHeight() <= this.contentPane.getHeight()) {
                    this.offset = 0.0;
                } else {
                    this.offset = this.contentPane.getHeight() - this.movePane.getPrefHeight();
                }

                this.requestLayout();
            }
        }

    }

    public void setContext(Node context) {
        this.movePane.getChildren().add(context);
        context.addEventHandler(MouseEvent.MOUSE_PRESSED, (event) -> {
            if (this.direction.get() == ToggleDirection.VERTICAL) {
                this.oldLocation = this.movePane.getLayoutY();
                this.mousePoint = event.getSceneY();
            }

        });
        context.addEventHandler(MouseDragEvent.MOUSE_DRAGGED, (event) -> {
            if (this.direction.get() == ToggleDirection.VERTICAL) {
                this.offset = event.getSceneY() - this.mousePoint + this.oldLocation;
                this.contentPane.requestLayout();
            }

        });
        context.addEventHandler(MouseDragEvent.MOUSE_RELEASED, (event) -> {
            this.resetLayout();
        });
    }

    class ToggleMovePane extends StackPane {
        ToggleMovePane() {
        }

        protected void layoutChildren() {
            double width = this.getWidth();
            ObservableList<Node> nodeList = this.getChildren();
            if (CustomerToggleGroup.this.direction.get() == ToggleDirection.VERTICAL) {
                for(int i = 0; i < nodeList.size(); ++i) {
                    ((Node)nodeList.get(i)).resizeRelocate(0.0, (double)i * (width + CustomerToggleGroup.this.spacing.get()), width, width);
                }
            }

        }
    }
}
